namespace DotNetNuke.UI.Skins
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Modules.Actions;
    using DotNetNuke.Entities.Modules.Communications;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Framework;
    using DotNetNuke.Security;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using DotNetNuke.UI.Containers;
    using DotNetNuke.UI.Skins.Controls;
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    /// -----------------------------------------------------------------------------
    /// Project	 : DotNetNuke
    /// Namespace: DotNetNuke.UI.Skins
    /// Class	 : Skin
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Skin is the base for the Skins 
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	07/04/2005	Documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public class Skin : UserControlBase
    {
        private ArrayList _actionEventListeners;
        private string CONTAINERLOAD_ERROR;
        private string CONTRACTEXPIRED_ERROR;
        private string CRITICAL_ERROR;
        private string MODULEACCESS_ERROR;
        private string MODULEADD_ERROR;
        private string MODULELOAD_ERROR;
        private ModuleCommunicate objCommunicator;
        private string PANE_LOAD_ERROR;
        private string TABACCESS_ERROR;

        public Skin()
        {
            base.Init += new EventHandler(this.Page_Init);
            this.objCommunicator = new ModuleCommunicate();
            this.PANE_LOAD_ERROR = DotNetNuke.Services.Localization.Localization.GetString("PaneNotFound.Error");
            this.CONTRACTEXPIRED_ERROR = DotNetNuke.Services.Localization.Localization.GetString("ContractExpired.Error");
            this.TABACCESS_ERROR = DotNetNuke.Services.Localization.Localization.GetString("TabAccess.Error");
            this.MODULEACCESS_ERROR = DotNetNuke.Services.Localization.Localization.GetString("ModuleAccess.Error");
            this.CRITICAL_ERROR = DotNetNuke.Services.Localization.Localization.GetString("CriticalError.Error");
            this.MODULELOAD_ERROR = DotNetNuke.Services.Localization.Localization.GetString("ModuleLoad.Error");
            this.MODULEADD_ERROR = DotNetNuke.Services.Localization.Localization.GetString("ModuleAdd.Error");
            this.CONTAINERLOAD_ERROR = DotNetNuke.Services.Localization.Localization.GetString("ContainerLoad.Error");
        }

        public static void AddModuleMessage(PortalModuleBase objPortalModuleBase, string Message, ModuleMessage.ModuleMessageType objModuleMessageType)
        {
            AddModuleMessage(objPortalModuleBase, "", Message, objModuleMessageType);
        }

        public static void AddModuleMessage(PortalModuleBase objPortalModuleBase, string Heading, string Message, ModuleMessage.ModuleMessageType objModuleMessageType)
        {
            if ((objPortalModuleBase != null) && (Message != ""))
            {
                PlaceHolder MessagePlaceHolder = (PlaceHolder) objPortalModuleBase.Parent.FindControl("MessagePlaceHolder");
                if (MessagePlaceHolder != null)
                {
                    MessagePlaceHolder.Visible = true;
                    ModuleMessage objModuleMessage = GetModuleMessageControl(Heading, Message, objModuleMessageType);
                    MessagePlaceHolder.Controls.Add(objModuleMessage);
                    ((CDefault) objPortalModuleBase.Page).ScrollToControl(MessagePlaceHolder.Parent);
                }
            }
        }

        public static void AddModuleMessage(PortalModuleBase objPortalModuleBase, string Heading, string Message, string IconSrc)
        {
            if ((objPortalModuleBase != null) && (Message != ""))
            {
                PlaceHolder MessagePlaceHolder = (PlaceHolder) objPortalModuleBase.Parent.FindControl("MessagePlaceHolder");
                if (MessagePlaceHolder != null)
                {
                    MessagePlaceHolder.Visible = true;
                    ModuleMessage objModuleMessage = GetModuleMessageControl(Heading, Message, IconSrc);
                    MessagePlaceHolder.Controls.Add(objModuleMessage);
                }
            }
        }

        public static void AddPageMessage(Skin objSkin, string Heading, string Message, ModuleMessage.ModuleMessageType objModuleMessageType)
        {
            if (Message != "")
            {
                Control ContentPane = objSkin.FindControl("ContentPane");
                if (ContentPane != null)
                {
                    ModuleMessage objModuleMessage = GetModuleMessageControl(Heading, Message, objModuleMessageType);
                    ContentPane.Controls.AddAt(0, objModuleMessage);
                }
            }
        }

        public static void AddPageMessage(Skin objSkin, string Heading, string Message, string IconSrc)
        {
            if (Message != "")
            {
                Control ContentPane = objSkin.FindControl("ContentPane");
                if (ContentPane != null)
                {
                    ModuleMessage objModuleMessage = GetModuleMessageControl(Heading, Message, IconSrc);
                    ContentPane.Controls.AddAt(0, objModuleMessage);
                }
            }
        }

        public static void AddPageMessage(Page objPage, string Heading, string Message, ModuleMessage.ModuleMessageType objModuleMessageType)
        {
            if (Message != "")
            {
                Control ContentPane = objPage.FindControl("ContentPane");
                if (ContentPane != null)
                {
                    ModuleMessage objModuleMessage = GetModuleMessageControl(Heading, Message, objModuleMessageType);
                    ContentPane.Controls.AddAt(0, objModuleMessage);
                }
            }
        }

        public static void AddPageMessage(Page objPage, string Heading, string Message, string IconSrc)
        {
            if (Message != "")
            {
                Control ContentPane = objPage.FindControl("ContentPane");
                if (ContentPane != null)
                {
                    ModuleMessage objModuleMessage = GetModuleMessageControl(Heading, Message, IconSrc);
                    ContentPane.Controls.AddAt(0, objModuleMessage);
                }
            }
        }

        private void CollapseUnusedPanes()
        {
            IEnumerator refObjectHelperL0=null;
            try
            {
                refObjectHelperL0 = this.PortalSettings.ActiveTab.Panes.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    string strPane = Conversions.ToString(refObjectHelperL0.Current);
                    Control ctlPane = this.FindControl(strPane);
                    if (ctlPane != null)
                    {
                        if (!ctlPane.HasControls())
                        {
                            HtmlControl objHtmlControl = (HtmlControl) ctlPane;
                            objHtmlControl.Attributes["width"] = "0";
                            if (objHtmlControl.Attributes["style"] != null)
                            {
                                objHtmlControl.Attributes.Remove("style");
                            }
                        }
                        else if (((ctlPane.Controls.Count == 1) && (ctlPane.Controls[0].GetType() == typeof(Label))) && (((Label) ctlPane.Controls[0]).Text.LastIndexOf(ctlPane.ID) > 0))
                        {
                            HtmlControl objHtmlControl = (HtmlControl) ctlPane;
                            objHtmlControl.Attributes["width"] = "0";
                            if (objHtmlControl.Attributes["style"] != null)
                            {
                                objHtmlControl.Attributes.Remove("style");
                            }
                            Label objLabel = (Label) ctlPane.Controls[0];
                            objLabel.Visible = false;
                        }
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        }

        private Control FindActionControl(Control objContainer)
        {
            IEnumerator refObjectHelperL0=null;
            try
            {
                refObjectHelperL0 = objContainer.Controls.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    Control objChildControl = (Control) refObjectHelperL0.Current;
                    if (objChildControl is ActionBase)
                    {
                        return objChildControl;
                    }
                    if (objChildControl.HasControls())
                    {
                        Control objFoundControl = this.FindActionControl(objChildControl);
                        if (objFoundControl != null)
                        {
                            return objFoundControl;
                        }
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return null;
        }

        public static ModuleMessage GetModuleMessageControl(string Heading, string Message, ModuleMessage.ModuleMessageType objModuleMessageType)
        {
            Skin s = new Skin();
            ModuleMessage objModuleMessage = (ModuleMessage) s.LoadControl("~/admin/skins/ModuleMessage.ascx");
            objModuleMessage.Heading = Heading;
            objModuleMessage.Text = Message;
            objModuleMessage.IconType = objModuleMessageType;
            return objModuleMessage;
        }

        public static ModuleMessage GetModuleMessageControl(string Heading, string Message, string IconImage)
        {
            Skin s = new Skin();
            ModuleMessage objModuleMessage = (ModuleMessage) s.LoadControl("~/admin/skins/ModuleMessage.ascx");
            objModuleMessage.Heading = Heading;
            objModuleMessage.Text = Message;
            objModuleMessage.IconImage = IconImage;
            return objModuleMessage;
        }

        public static Skin GetParentSkin(PortalModuleBase objModule)
        {
            Control MyParent = objModule.Parent;
            bool FoundSkin = false;
            while (MyParent != null)
            {
                if (MyParent is Skin)
                {
                    FoundSkin = true;
                    break;
                }
                MyParent = MyParent.Parent;
            }
            if (FoundSkin)
            {
                return (Skin) MyParent;
            }
            return null;
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        public void InjectModule(Control objPane, DotNetNuke.Entities.Modules.ModuleInfo objModule, PortalSettings PortalSettings)
        {
            try
            {
                string ID;
                HtmlGenericControl objLink;
                PortalModuleBase objPortalModuleBase = null;
                UserControl ctlContainer = null;
                SkinController objSkins = new SkinController();
                string strOldContainerSource = objModule.ContainerSrc;
                string strOldContainerPath = objModule.ContainerPath;
                int PreviewModuleId = -1;
                if (this.Request.QueryString["ModuleId"] != null)
                {
                    PreviewModuleId = int.Parse(this.Request.QueryString["ModuleId"]);
                }
                if ((this.Request.QueryString["ContainerSrc"] != null) & ((objModule.ModuleID == PreviewModuleId) | (PreviewModuleId == -1)))
                {
                    objModule.ContainerSrc = SkinController.FormatSkinSrc(DotNetNuke.Common.Globals.QueryStringDecode(this.Request.QueryString["ContainerSrc"]) + ".ascx", PortalSettings);
                    ctlContainer = this.LoadContainer(objModule.ContainerSrc, objPane);
                }
                if (((ctlContainer == null) && (this.Request.Cookies["_ContainerSrc" + PortalSettings.PortalId.ToString()] != null)) && (this.Request.Cookies["_ContainerSrc" + PortalSettings.PortalId.ToString()].Value != ""))
                {
                    objModule.ContainerSrc = SkinController.FormatSkinSrc(this.Request.Cookies["_ContainerSrc" + PortalSettings.PortalId.ToString()].Value + ".ascx", PortalSettings);
                    ctlContainer = this.LoadContainer(objModule.ContainerSrc, objPane);
                }
                if ((ctlContainer == null) && !objModule.DisplayTitle)
                {
                    bool blnDisplayTitle = (PortalSecurity.IsInRoles(PortalSettings.AdministratorRoleName) | PortalSecurity.IsInRoles(PortalSettings.ActiveTab.AdministratorRoles.ToString())) | DotNetNuke.Common.Globals.IsAdminSkin(PortalSettings.ActiveTab.IsAdminTab);
                    if (blnDisplayTitle && (this.Request.Cookies["_Tab_Admin_Preview" + PortalSettings.PortalId.ToString()] != null))
                    {
                        blnDisplayTitle = !bool.Parse(this.Request.Cookies["_Tab_Admin_Preview" + PortalSettings.PortalId.ToString()].Value);
                    }
                    if (!blnDisplayTitle)
                    {
                        objModule.ContainerSrc = SkinController.FormatSkinSrc("[G]" + SkinInfo.RootContainer + "/_default/No Container.ascx", PortalSettings);
                        ctlContainer = this.LoadContainer(objModule.ContainerSrc, objPane);
                    }
                }
                if (((ctlContainer == null) && (objModule.ContainerSrc == PortalSettings.ActiveTab.ContainerSrc)) && (objPane is HtmlControl))
                {
                    HtmlControl objHtmlControl = (HtmlControl) objPane;
                    if (objHtmlControl.Attributes["ContainerSrc"] != null)
                    {
                        if ((objHtmlControl.Attributes["ContainerType"] != null) & (objHtmlControl.Attributes["ContainerName"] != null))
                        {
                            objModule.ContainerSrc = "[" + objHtmlControl.Attributes["ContainerType"] + "]" + SkinInfo.RootContainer + "/" + objHtmlControl.Attributes["ContainerName"] + "/" + objHtmlControl.Attributes["ContainerSrc"];
                        }
                        else
                        {
                            objModule.ContainerSrc = objHtmlControl.Attributes["ContainerSrc"];
                        }
                        objModule.ContainerSrc = SkinController.FormatSkinSrc(objModule.ContainerSrc, PortalSettings);
                        ctlContainer = this.LoadContainer(objModule.ContainerSrc, objPane);
                    }
                }
                if (ctlContainer == null)
                {
                    if (DotNetNuke.Common.Globals.IsAdminSkin(PortalSettings.ActiveTab.IsAdminTab))
                    {
                        SkinInfo objSkin = SkinController.GetSkin(SkinInfo.RootContainer, PortalSettings.PortalId, SkinType.Admin);
                        if (objSkin != null)
                        {
                            objModule.ContainerSrc = objSkin.SkinSrc;
                        }
                        else
                        {
                            objModule.ContainerSrc = "";
                        }
                    }
                    if (objModule.ContainerSrc != "")
                    {
                        objModule.ContainerSrc = SkinController.FormatSkinSrc(objModule.ContainerSrc, PortalSettings);
                        ctlContainer = this.LoadContainer(objModule.ContainerSrc, objPane);
                    }
                }
                if (ctlContainer == null)
                {
                    if (DotNetNuke.Common.Globals.IsAdminSkin(PortalSettings.ActiveTab.IsAdminTab))
                    {
                        objModule.ContainerSrc = DotNetNuke.Common.Globals.HostPath + SkinInfo.RootContainer + "/DNN-Blue/Image Header - Color Background.ascx";
                    }
                    else
                    {
                        objModule.ContainerSrc = DotNetNuke.Common.Globals.HostPath + SkinInfo.RootContainer + "/DNN-Blue/Image Header - Color Background.ascx";
                    }
                    ctlContainer = this.LoadContainer(objModule.ContainerSrc, objPane);
                }
                objModule.ContainerPath = SkinController.FormatSkinPath(objModule.ContainerSrc);
                Control objCSS = this.Page.FindControl("CSS");
                Hashtable objCSSCache = null;
                if (DotNetNuke.Common.Globals.PerformanceSetting != DotNetNuke.Common.Globals.PerformanceSettings.NoCaching)
                {
                    objCSSCache = (Hashtable) DotNetNuke.Common.Utilities.DataCache.GetCache("CSS");
                }
                if (objCSSCache == null)
                {
                    objCSSCache = new Hashtable();
                }
                if (objCSS != null)
                {
                    ID = DotNetNuke.Common.Globals.CreateValidID(objModule.ContainerPath);
                    if (!objCSSCache.ContainsKey(ID))
                    {
                        if (File.Exists(this.Server.MapPath(objModule.ContainerPath) + "container.css"))
                        {
                            objCSSCache[ID] = objModule.ContainerPath + "container.css";
                        }
                        else
                        {
                            objCSSCache[ID] = "";
                        }
                        if (DotNetNuke.Common.Globals.PerformanceSetting != DotNetNuke.Common.Globals.PerformanceSettings.NoCaching)
                        {
                            DotNetNuke.Common.Utilities.DataCache.SetCache("CSS", objCSSCache);
                        }
                    }
                    if ((objCSSCache[ID].ToString() != "") && (objCSS.FindControl(ID) == null))
                    {
                        objLink = new HtmlGenericControl("LINK");
                        objLink.ID = ID;
                        objLink.Attributes["rel"] = "stylesheet";
                        objLink.Attributes["type"] = "text/css";
                        objLink.Attributes["href"] = objCSSCache[ID].ToString();
                        objCSS.Controls.Add(objLink);
                    }
                    ID = DotNetNuke.Common.Globals.CreateValidID(objModule.ContainerSrc.Replace(".ascx", ".css"));
                    if (!objCSSCache.ContainsKey(ID))
                    {
                        if (File.Exists(this.Server.MapPath(Strings.Replace(objModule.ContainerSrc, ".ascx", ".css", 1, -1, CompareMethod.Binary))))
                        {
                            objCSSCache[ID] = Strings.Replace(objModule.ContainerSrc, ".ascx", ".css", 1, -1, CompareMethod.Binary);
                        }
                        else
                        {
                            objCSSCache[ID] = "";
                        }
                        if (DotNetNuke.Common.Globals.PerformanceSetting != DotNetNuke.Common.Globals.PerformanceSettings.NoCaching)
                        {
                            DotNetNuke.Common.Utilities.DataCache.SetCache("CSS", objCSSCache);
                        }
                    }
                    if ((objCSSCache[ID].ToString() != "") && (objCSS.FindControl(ID) == null))
                    {
                        objLink = new HtmlGenericControl("LINK");
                        objLink.ID = ID;
                        objLink.Attributes["rel"] = "stylesheet";
                        objLink.Attributes["type"] = "text/css";
                        objLink.Attributes["href"] = objCSSCache[ID].ToString();
                        objCSS.Controls.Add(objLink);
                    }
                }
                if (!DotNetNuke.Common.Globals.IsAdminControl())
                {
                    objPane.Controls.Add(new LiteralControl("<a name=\"" + objModule.ModuleID.ToString() + "\"></a>"));
                }
                Control objCell = ctlContainer.FindControl("ContentPane");
                if (objCell != null)
                {
                    if (objCell is HtmlContainerControl)
                    {
                        if (objModule.Alignment != "")
                        {
                            ((HtmlContainerControl) objCell).Attributes.Add("align", objModule.Alignment);
                        }
                        if (objModule.Color != "")
                        {
                            ((HtmlContainerControl) objCell).Style["background"] = objModule.Color;
                        }
                        if (objModule.Border != "")
                        {
                            ((HtmlContainerControl) objCell).Style["border-top"] = objModule.Border + "px #000000 solid";
                            ((HtmlContainerControl) objCell).Style["border-bottom"] = objModule.Border + "px #000000 solid";
                            ((HtmlContainerControl) objCell).Style["border-right"] = objModule.Border + "px #000000 solid";
                            ((HtmlContainerControl) objCell).Style["border-left"] = objModule.Border + "px #000000 solid";
                        }
                        if ((!DotNetNuke.Common.Globals.IsAdminControl() & !PortalSettings.ActiveTab.IsAdminTab) && (((DateTime.Compare(objModule.StartDate, DateAndTime.Now) >= 0) | (DateTime.Compare(objModule.EndDate, DateAndTime.Now) <= 0)) | (objModule.AuthorizedViewRoles.ToLower() == (";" + PortalSettings.AdministratorRoleName.ToLower() + ";"))))
                        {
                            ((HtmlContainerControl) objCell).Style["border-top"] = "2px #FF0000 solid";
                            ((HtmlContainerControl) objCell).Style["border-bottom"] = "2px #FF0000 solid";
                            ((HtmlContainerControl) objCell).Style["border-right"] = "2px #FF0000 solid";
                            ((HtmlContainerControl) objCell).Style["border-left"] = "2px #FF0000 solid";
                            objCell.Controls.Add(new LiteralControl("<span class=\"NormalRed\"><center>" + DotNetNuke.Services.Localization.Localization.GetString("ModuleVisibleAdministrator.Text") + "</center></span>"));
                        }
                    }
                    if (!DotNetNuke.Common.Globals.IsAdminControl())
                    {
                        objCell.Controls.Add(new LiteralControl("<!-- Start_Module_" + objModule.ModuleID.ToString() + " -->"));
                    }
                    if (objModule.Header != "")
                    {
                        Label objLabel = new Label();
                        objLabel.Text = objModule.Header;
                        objLabel.CssClass = "Normal";
                        objCell.Controls.Add(objLabel);
                    }
                    PlaceHolder MessagePlaceholder = new PlaceHolder();
                    MessagePlaceholder.ID = "MessagePlaceHolder";
                    MessagePlaceholder.Visible = false;
                    objCell.Controls.Add(MessagePlaceholder);
                    Panel objPanel = new Panel();
                    objPanel.ID = "ModuleContent";
                    bool blnContent = true;
                    if (this.Request.Cookies["_Tab_Admin_Content" + PortalSettings.PortalId.ToString()] != null)
                    {
                        blnContent = bool.Parse(this.Request.Cookies["_Tab_Admin_Content" + PortalSettings.PortalId.ToString()].Value);
                    }
                    if (this.Request.QueryString["content"] != null)
                    {
                        string refStringHelperL0 = this.Request.QueryString["Content"].ToLower();
                        if ((((refStringHelperL0 == "1") || (refStringHelperL0 == "true")) ? 1 : 0) != 0)
                        {
                            blnContent = true;
                        }
                        else if ((((refStringHelperL0 == "0") || (refStringHelperL0 == "false")) ? 1 : 0) != 0)
                        {
                            blnContent = false;
                        }
                    }
                    if (DotNetNuke.Common.Globals.IsAdminControl() | PortalSettings.ActiveTab.IsAdminTab)
                    {
                        blnContent = true;
                    }
                    try
                    {
                        if (blnContent)
                        {
                            if ((objModule.CacheTime != 0) & (!PortalSecurity.IsInRoles(PortalSettings.AdministratorRoleName) & !PortalSecurity.IsInRoles(PortalSettings.ActiveTab.AdministratorRoles.ToString())))
                            {
                                objPortalModuleBase = new PortalModuleBase();
                            }
                            else
                            {
                                objPortalModuleBase = (PortalModuleBase) this.LoadControl("~/" + objModule.ControlSrc);
                            }
                            objPortalModuleBase.ID = Path.GetFileNameWithoutExtension(objModule.ControlSrc);
                        }
                        else
                        {
                            objPortalModuleBase = (PortalModuleBase) this.LoadControl("~/admin/Portal/NoContent.ascx");
                        }
                        this.objCommunicator.LoadCommunicator(objPortalModuleBase);
                        objPortalModuleBase.ModuleConfiguration = objModule;
                    }
                    catch (ThreadAbortException exception1)
                    {
                        
                        ThreadAbortException exc = exception1;
                        Thread.ResetAbort();
                        
                    }
                    catch (Exception exception2)
                    {
                        ProjectData.SetProjectError(exception2);
                        Exception exc = exception2;
                        if (objPortalModuleBase == null)
                        {
                            DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this.MODULELOAD_ERROR, objPanel, exc);
                        }
                        else
                        {
                            DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(objPortalModuleBase, exc);
                        }
                        
                    }
                    if (objPortalModuleBase != null)
                    {
                        objPanel.Controls.Add(objPortalModuleBase);
                    }
                    objCell.Controls.Add(objPanel);
                    if (objPortalModuleBase != null)
                    {
                        objPortalModuleBase.FindControl("");
                        Panel objModuleContent = (Panel) objPortalModuleBase.FindControl("ModuleContent");
                        if (objModuleContent != null)
                        {
                            objModuleContent.Visible = false;
                        }
                    }
                    if (objModule.Footer != "")
                    {
                        Label objLabel = new Label();
                        objLabel.Text = objModule.Footer;
                        objLabel.CssClass = "Normal";
                        objCell.Controls.Add(objLabel);
                    }
                    if (!DotNetNuke.Common.Globals.IsAdminControl())
                    {
                        objPanel.Controls.Add(new LiteralControl("<!-- End_Module_" + objModule.ModuleID.ToString() + " -->"));
                    }
                }
                ctlContainer.ID = "ctr";
                if (objPortalModuleBase != null)
                {
                    UserControl refObjectHelperS0 = ctlContainer;
                    refObjectHelperS0.ID = refObjectHelperS0.ID + objPortalModuleBase.ModuleId.ToString();
                }
                if (((((objPortalModuleBase != null) && DotNetNuke.Common.Globals.IsLayoutMode()) && !DotNetNuke.Common.Globals.IsAdminControl()) ? 1 : 0) != 0)
                {
                    Panel ctlDragDropContainer = new Panel();
                    Control ctlTitle = ctlContainer.FindControl("dnnTitle");
                    ctlDragDropContainer.ID = ctlContainer.ID + "_DD";
                    objPane.Controls.Add(ctlDragDropContainer);
                    ctlDragDropContainer.Controls.Add(ctlContainer);
                    if ((ctlTitle != null) && (ctlTitle.Controls.Count > 0))
                    {
                        ctlTitle = ctlTitle.Controls[0];
                    }
                    if ((((ctlDragDropContainer != null) && (ctlTitle != null)) ? 1 : 0) != 0)
                    {
                        DNNClientAPI.EnableContainerDragAndDrop(ctlTitle, ctlDragDropContainer, objPortalModuleBase.ModuleId);
                        ClientAPI.RegisterPostBackEventHandler(objCell, "MoveToPane", new ClientAPIPostBackControl.PostBackEvent(this.ModuleMoveToPanePostBack), false);
                    }
                }
                else
                {
                    objPane.Controls.Add(ctlContainer);
                }
                if (objPortalModuleBase != null)
                {
                    ActionBase objActions = (ActionBase) this.FindActionControl(ctlContainer);
                    if (objActions != null)
                    {
                        objActions.ModuleConfiguration = objPortalModuleBase.ModuleConfiguration;
                        ModuleActionCollection arrActions = objPortalModuleBase.Actions;
                        if (arrActions != null)
                        {
                            if (objPortalModuleBase is IActionable)
                            {
                                arrActions.AddRange(((IActionable) objPortalModuleBase).ModuleActions);
                            }
                            if ((objModule.IsPortable & !DotNetNuke.Common.Globals.IsAdminControl()) & (objModule.BusinessControllerClass != ""))
                            {
                                arrActions.Insert(0, new ModuleAction(arrActions.Count + 1, DotNetNuke.Services.Localization.Localization.GetString("ImportModule.Action", "~/App_GlobalResources/GlobalResources.resx"), "", "", "rt.gif", DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "ImportModule", new string[] { "moduleid=" + objModule.ModuleID.ToString() }), "", false, SecurityAccessLevel.Admin, objActions.EditMode, false));
                                arrActions.Insert(0, new ModuleAction(arrActions.Count + 1, DotNetNuke.Services.Localization.Localization.GetString("ExportModule.Action", "~/App_GlobalResources/GlobalResources.resx"), "", "", "lt.gif", DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "ExportModule", new string[] { "moduleid=" + objModule.ModuleID.ToString() }), "", false, SecurityAccessLevel.Admin, objActions.EditMode, false));
                            }
                            if ((objModule.IsSearchable & !DotNetNuke.Common.Globals.IsAdminControl()) & (objModule.BusinessControllerClass != ""))
                            {
                                arrActions.Insert(0, new ModuleAction(arrActions.Count + 1, DotNetNuke.Services.Localization.Localization.GetString("SyndicateModule.Action", "~/App_GlobalResources/GlobalResources.resx"), "SyndicateModule.Action", "", "xml.gif", DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "", new string[] { "moduleid=" + objModule.ModuleID.ToString() }).Replace("Default.aspx", "RSS.aspx"), "", false, SecurityAccessLevel.Anonymous, true, true));
                            }
                            objActions.MenuActions.AddRange(arrActions);
                        }
                        objActions.Action += new ActionEventHandler(this.ModuleAction_Click);
                    }
                    ID = DotNetNuke.Common.Globals.CreateValidID(DotNetNuke.Common.Globals.ApplicationPath + "/" + objModule.ControlSrc.Substring(0, objModule.ControlSrc.LastIndexOf("/")));
                    if (!objCSSCache.ContainsKey(ID))
                    {
                        if (File.Exists(this.Server.MapPath(DotNetNuke.Common.Globals.ApplicationPath + "/" + objModule.ControlSrc.Substring(0, objModule.ControlSrc.LastIndexOf("/") + 1)) + "module.css"))
                        {
                            objCSSCache[ID] = DotNetNuke.Common.Globals.ApplicationPath + "/" + objModule.ControlSrc.Substring(0, objModule.ControlSrc.LastIndexOf("/") + 1) + "module.css";
                        }
                        else
                        {
                            objCSSCache[ID] = "";
                        }
                        if (DotNetNuke.Common.Globals.PerformanceSetting != DotNetNuke.Common.Globals.PerformanceSettings.NoCaching)
                        {
                            DotNetNuke.Common.Utilities.DataCache.SetCache("CSS", objCSSCache);
                        }
                    }
                    if (((objCSSCache[ID].ToString() != "") && (objCSS != null)) && (objCSS.FindControl(ID) == null))
                    {
                        objLink = new HtmlGenericControl("LINK");
                        objLink.ID = ID;
                        objLink.Attributes["rel"] = "stylesheet";
                        objLink.Attributes["type"] = "text/css";
                        objLink.Attributes["href"] = objCSSCache[ID].ToString();
                        objCSS.Controls.AddAt(0, objLink);
                    }
                }
                if (!objPane.Visible)
                {
                    objPane.Visible = true;
                }
                objModule.ContainerPath = strOldContainerPath;
                objModule.ContainerSrc = strOldContainerSource;
            }
            catch (Exception exception3)
            {
                ProjectData.SetProjectError(exception3);
                Exception exc = exception3;
                ModuleLoadException lex = new ModuleLoadException(string.Format(this.MODULEADD_ERROR, objPane.ID.ToString()), exc);
                objPane.Controls.Add(new ErrorContainer(PortalSettings, this.MODULELOAD_ERROR, lex).Container);
                DotNetNuke.Services.Exceptions.Exceptions.LogException(lex);
                Information.Err().Clear();
                
            }
        }

        private UserControl LoadContainer(string ContainerPath, Control objPane)
        {
            if ((((ContainerPath.ToLower().IndexOf("/skins/") != -1) | (ContainerPath.ToLower().IndexOf(@"/skins\") != -1)) | (ContainerPath.ToLower().IndexOf(@"\skins\") != -1)) | (ContainerPath.ToLower().IndexOf(@"\skins/") != -1))
            {
                throw new Exception();
            }
            UserControl ctlContainer = null;
            try
            {
                if (ContainerPath.ToLower().IndexOf(DotNetNuke.Common.Globals.ApplicationPath.ToLower()) != -1)
                {
                    ContainerPath = ContainerPath.Remove(0, Strings.Len(DotNetNuke.Common.Globals.ApplicationPath));
                }
                ctlContainer = (UserControl) this.LoadControl("~" + ContainerPath);
                ctlContainer.DataBind();
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                ModuleLoadException lex = new ModuleLoadException(this.MODULELOAD_ERROR, exc);
                if (PortalSecurity.IsInRoles(this.PortalSettings.AdministratorRoleName) | PortalSecurity.IsInRoles(this.PortalSettings.ActiveTab.AdministratorRoles.ToString()))
                {
                    objPane.Controls.Add(new ErrorContainer(this.PortalSettings, string.Format(this.CONTAINERLOAD_ERROR, ContainerPath), lex).Container);
                }
                DotNetNuke.Services.Exceptions.Exceptions.LogException(lex);
                Information.Err().Clear();
                
            }
            return ctlContainer;
        }

        public void ModuleAction_Click(object sender, ActionEventArgs e)
        {
            IEnumerator refObjectHelperL0=null;
            try
            {
                refObjectHelperL0 = this.ActionEventListeners.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    ModuleActionEventListener Listener = (ModuleActionEventListener) refObjectHelperL0.Current;
                    if (e.ModuleConfiguration.ModuleID == Listener.ModuleID)
                    {
                        Listener.ActionEvent(RuntimeHelpers.GetObjectValue(sender), e);
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
        }

        private void ModuleMoveToPanePostBack(ClientAPIPostBackEventArgs args)
        {
            PortalSettings PortalSettings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];
            if (PortalSecurity.IsInRole(PortalSettings.AdministratorRoleName.ToString()) | PortalSecurity.IsInRoles(PortalSettings.ActiveTab.AdministratorRoles.ToString()))
            {
                int intModuleID = Conversions.ToInteger(args.EventArguments["moduleid"]);
                string strPaneName = Conversions.ToString(args.EventArguments["pane"]);
                int intOrder = Conversions.ToInteger(args.EventArguments["order"]);
                ModuleController objModules = new ModuleController();
                objModules.UpdateModuleOrder(PortalSettings.ActiveTab.TabID, intModuleID, intOrder, strPaneName);
                objModules.UpdateTabModuleOrder(PortalSettings.ActiveTab.TabID, PortalSettings.ActiveTab.PortalID);
                this.Response.Redirect(this.Request.RawUrl, true);
            }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            Control ctlPane;
            IEnumerator refObjectHelperL0=null;
            this.InitializeComponent();
            ModuleController objModules = new ModuleController();
            DotNetNuke.Entities.Modules.ModuleInfo objModule = null;
            bool blnLayoutMode = DotNetNuke.Common.Globals.IsLayoutMode();
            try
            {
                refObjectHelperL0 = this.Controls.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    Control ctlControl = (Control) refObjectHelperL0.Current;
                    if (ctlControl is HtmlControl)
                    {
                        HtmlControl objHtmlControl = (HtmlControl) ctlControl;
                        if (objHtmlControl.ID != null)
                        {
                            string refStringHelperL0 = objHtmlControl.TagName.ToUpper();
                            if (((refStringHelperL0 == "TD") || (refStringHelperL0 == "DIV")) || (refStringHelperL0 == "SPAN"))
                            {
                            }
                            if ((((refStringHelperL0 == "P") ? 1 : 0) != 0) && (ctlControl.ID != "ControlPanel"))
                            {
                                this.PortalSettings.ActiveTab.Panes.Add(ctlControl.ID);
                            }
                        }
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            if ((this.Request.QueryString["dnnprintmode"] != "true") && (PortalSecurity.IsInRoles(this.PortalSettings.AdministratorRoleName.ToString()) | PortalSecurity.IsInRoles(this.PortalSettings.ActiveTab.AdministratorRoles.ToString())))
            {
                UserControl objControlPanel = null;
                if (Convert.ToString(RuntimeHelpers.GetObjectValue(this.PortalSettings.HostSettings["ControlPanel"])) != "")
                {
                    objControlPanel = (UserControl) this.LoadControl("~/" + Convert.ToString(RuntimeHelpers.GetObjectValue(this.PortalSettings.HostSettings["ControlPanel"])));
                }
                if (objControlPanel == null)
                {
                    objControlPanel = (UserControl) this.LoadControl("~/Admin/ControlPanel/IconBar.ascx");
                }
                ctlPane = this.FindControl("ControlPanel");
                if (ctlPane == null)
                {
                    HtmlForm objForm = (HtmlForm) this.Parent.FindControl("Form");
                    objForm.Controls.AddAt(0, objControlPanel);
                }
                else
                {
                    ctlPane.Controls.Add(objControlPanel);
                }
            }
            if (!DotNetNuke.Common.Globals.IsAdminControl())
            {
                if (PortalSecurity.IsInRoles(this.PortalSettings.ActiveTab.AuthorizedRoles))
                {
                    bool blnExpired = false;
                    if ((DateTime.Compare(this.PortalSettings.ExpiryDate, Null.NullDate) != 0) && (((DateTime.Compare(Convert.ToDateTime(this.PortalSettings.ExpiryDate), DateAndTime.Now) < 0) & (this.PortalSettings.ActiveTab.ParentId != this.PortalSettings.AdminTabId)) & (this.PortalSettings.ActiveTab.ParentId != this.PortalSettings.SuperTabId)))
                    {
                        blnExpired = true;
                    }
                    if (!blnExpired)
                    {
                        if (((DateTime.Compare(this.PortalSettings.ActiveTab.StartDate, DateAndTime.Now) < 0) & (DateTime.Compare(this.PortalSettings.ActiveTab.EndDate, DateAndTime.Now) > 0)) | blnLayoutMode)
                        {
                            if (blnLayoutMode)
                            {
                                IEnumerator refObjectHelperL1=null;
                                try
                                {
                                    refObjectHelperL1 = this.PortalSettings.ActiveTab.Panes.GetEnumerator();
                                    while (refObjectHelperL1.MoveNext())
                                    {
                                        string strPane = Conversions.ToString(refObjectHelperL1.Current);
                                        ctlPane = this.FindControl(strPane);
                                        ctlPane.Visible = true;
                                        if (ctlPane is HtmlContainerControl)
                                        {
                                            ((HtmlContainerControl) ctlPane).Style["border-top"] = "1px #CCCCCC dotted";
                                            ((HtmlContainerControl) ctlPane).Style["border-bottom"] = "1px #CCCCCC dotted";
                                            ((HtmlContainerControl) ctlPane).Style["border-right"] = "1px #CCCCCC dotted";
                                            ((HtmlContainerControl) ctlPane).Style["border-left"] = "1px #CCCCCC dotted";
                                        }
                                        Label ctlLabel = new Label();
                                        ctlLabel.Text = "<center>" + strPane + "</center><br>";
                                        ctlLabel.CssClass = "SubHead";
                                        ctlPane.Controls.AddAt(0, ctlLabel);
                                    }
                                }
                                finally
                                {
                                    if (refObjectHelperL1 is IDisposable)
                                    {
                                        (refObjectHelperL1 as IDisposable).Dispose();
                                    }
                                }
                            }
                            if (this.PortalSettings.ActiveTab.Modules.Count > 0)
                            {
                                IEnumerator refObjectHelperL2=null;
                                try
                                {
                                    refObjectHelperL2 = this.PortalSettings.ActiveTab.Modules.GetEnumerator();
                                    while (refObjectHelperL2.MoveNext())
                                    {
                                        objModule = (DotNetNuke.Entities.Modules.ModuleInfo) refObjectHelperL2.Current;
                                        if (((PortalSecurity.IsInRoles(objModule.AuthorizedViewRoles) & !objModule.IsDeleted) && (((DateTime.Compare(objModule.StartDate, DateAndTime.Now) < 0) & (DateTime.Compare(objModule.EndDate, DateAndTime.Now) > 0)) | blnLayoutMode)) && (!objModule.AllTabs | !this.PortalSettings.ActiveTab.IsAdminTab))
                                        {
                                            Control parent = this.FindControl(objModule.PaneName);
                                            if (parent == null)
                                            {
                                                parent = this.FindControl("ContentPane");
                                            }
                                            if (parent != null)
                                            {
                                                if (this.PortalSettings.ActiveTab.IsAdminTab)
                                                {
                                                    objModule.ModuleTitle = DotNetNuke.Services.Localization.Localization.LocalizeControlTitle(objModule.ModuleTitle, objModule.ControlSrc, "");
                                                }
                                                this.InjectModule(parent, objModule, this.PortalSettings);
                                            }
                                            else
                                            {
                                                ModuleLoadException lex = new ModuleLoadException(this.PANE_LOAD_ERROR);
                                                this.Controls.Add(new ErrorContainer(this.PortalSettings, this.MODULELOAD_ERROR, lex).Container);
                                                DotNetNuke.Services.Exceptions.Exceptions.LogException(lex);
                                                Information.Err().Clear();
                                            }
                                        }
                                    }
                                }
                                finally
                                {
                                    if (refObjectHelperL2 is IDisposable)
                                    {
                                        (refObjectHelperL2 as IDisposable).Dispose();
                                    }
                                }
                            }
                        }
                        else
                        {
                            AddPageMessage(this, "", this.TABACCESS_ERROR, ModuleMessage.ModuleMessageType.YellowWarning);
                        }
                    }
                    else
                    {
                        AddPageMessage(this, "", string.Format(this.CONTRACTEXPIRED_ERROR, this.PortalSettings.PortalName, DotNetNuke.Common.Globals.GetMediumDate(this.PortalSettings.ExpiryDate.ToString()), this.PortalSettings.Email), ModuleMessage.ModuleMessageType.RedError);
                    }
                }
                else if (this.Request.IsAuthenticated)
                {
                    AddPageMessage(this, "", this.TABACCESS_ERROR, ModuleMessage.ModuleMessageType.YellowWarning);
                }
                else if (this.PortalSettings.LoginTabId != -1)
                {
                    this.Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.PortalSettings.LoginTabId), true);
                }
                else
                {
                    this.Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("Login"), true);
                }
            }
            else
            {
                int ModuleId = -1;
                string Key = "";
                if (!Information.IsNothing(this.Request.QueryString["mid"]))
                {
                    ModuleId = int.Parse(this.Request.QueryString["mid"]);
                }
                if (!Information.IsNothing(this.Request.QueryString["ctl"]))
                {
                    Key = this.Request.QueryString["ctl"];
                }
                if (!Information.IsNothing(this.Request.QueryString["moduleid"]) & ((Key.ToLower() == "module") | (Key.ToLower() == "help")))
                {
                    ModuleId = int.Parse(this.Request.QueryString["moduleid"]);
                }
                if (ModuleId != -1)
                {
                    objModule = objModules.GetModule(ModuleId, this.PortalSettings.ActiveTab.TabID);
                    if ((objModule != null) && objModule.InheritViewPermissions)
                    {
                        objModule.AuthorizedViewRoles = this.PortalSettings.ActiveTab.AuthorizedRoles;
                    }
                }
                if (objModule == null)
                {
                    objModule = new DotNetNuke.Entities.Modules.ModuleInfo();
                    objModule.ModuleID = ModuleId;
                    objModule.ModuleDefID = -1;
                    objModule.TabID = this.PortalSettings.ActiveTab.TabID;
                    objModule.AuthorizedEditRoles = "";
                    objModule.AuthorizedViewRoles = "";
                }
                if (!Information.IsNothing(this.Request.QueryString["moduleid"]) & ((Key.ToLower() == "module") | (Key.ToLower() == "help")))
                {
                    objModule.ModuleDefID = -1;
                }
                if (this.Request.QueryString["dnnprintmode"] != "true")
                {
                    objModule.ModuleTitle = "";
                }
                objModule.Header = "";
                objModule.Footer = "";
                objModule.StartDate = DateTime.MinValue;
                objModule.EndDate = DateTime.MaxValue;
                objModule.PaneName = "ContentPane";
                objModule.Visibility = VisibilityState.None;
                objModule.Color = "";
                if (this.Request.QueryString["dnnprintmode"] != "true")
                {
                    objModule.Alignment = "center";
                }
                objModule.Border = "";
                objModule.DisplayTitle = true;
                objModule.DisplayPrint = false;
                objModule.DisplaySyndicate = false;
                SkinInfo objSkin = SkinController.GetSkin(SkinInfo.RootContainer, this.PortalSettings.PortalId, SkinType.Portal);
                if (objSkin != null)
                {
                    objModule.ContainerSrc = objSkin.SkinSrc;
                }
                else
                {
                    objModule.ContainerSrc = "[G]" + SkinInfo.RootContainer + "/DNN-Blue/Image Header - Color Background.ascx";
                }
                objModule.ContainerSrc = SkinController.FormatSkinSrc(objModule.ContainerSrc, this.PortalSettings);
                objModule.ContainerPath = SkinController.FormatSkinPath(objModule.ContainerSrc);
                Control parent = this.FindControl(objModule.PaneName);
                ArrayList arrModuleControls = new ModuleControlController().GetModuleControlsByKey(Key, objModule.ModuleDefID);
                int refIntHelperL0 = arrModuleControls.Count - 1;
                for (int intCounter = 0; intCounter <= refIntHelperL0; intCounter++)
                {
                    ModuleControlInfo objModuleControl = (ModuleControlInfo) arrModuleControls[intCounter];
                    objModule.ModuleControlId = objModuleControl.ModuleControlID;
                    objModule.ControlSrc = objModuleControl.ControlSrc;
                    objModule.ControlType = objModuleControl.ControlType;
                    objModule.IconFile = objModuleControl.IconFile;
                    objModule.HelpUrl = objModuleControl.HelpURL;
                    if (!Null.IsNull(objModuleControl.ControlTitle))
                    {
                        objModule.ModuleTitle = DotNetNuke.Services.Localization.Localization.LocalizeControlTitle(objModuleControl.ControlTitle, objModule.ControlSrc, Key);
                    }
                    bool blnAuthorized = true;
                    switch (((int) objModule.ControlType))
                    {
                        case 0:
                            if ((!PortalSecurity.IsInRole(this.PortalSettings.AdministratorRoleName) & !PortalSecurity.IsInRoles(this.PortalSettings.ActiveTab.AdministratorRoles.ToString())) && !PortalSecurity.IsInRoles(objModule.AuthorizedViewRoles))
                            {
                                blnAuthorized = false;
                            }
                            goto Label_0D03;

                        case 1:
                            if (!PortalSecurity.IsInRole(this.PortalSettings.AdministratorRoleName) & !PortalSecurity.IsInRoles(this.PortalSettings.ActiveTab.AdministratorRoles.ToString()))
                            {
                                if (PortalSecurity.IsInRoles(objModule.AuthorizedViewRoles))
                                {
                                    break;
                                }
                                blnAuthorized = false;
                            }
                            goto Label_0D03;

                        case 2:
                            if (!PortalSecurity.IsInRole(this.PortalSettings.AdministratorRoleName) & !PortalSecurity.IsInRoles(this.PortalSettings.ActiveTab.AdministratorRoles.ToString()))
                            {
                                blnAuthorized = false;
                            }
                            goto Label_0D03;

                        case 3:
                            if (!UserController.GetCurrentUserInfo().IsSuperUser)
                            {
                                blnAuthorized = false;
                            }
                            goto Label_0D03;

                        default:
                            goto Label_0D03;
                    }
                    if (!PortalSecurity.HasEditPermissions(objModule.ModulePermissions))
                    {
                        blnAuthorized = false;
                    }
                Label_0D03:
                    if (blnAuthorized)
                    {
                        this.InjectModule(parent, objModule, this.PortalSettings);
                    }
                    else if (this.Request.IsAuthenticated)
                    {
                        AddPageMessage(this, "", this.MODULEACCESS_ERROR, ModuleMessage.ModuleMessageType.YellowWarning);
                    }
                    else if (this.PortalSettings.LoginTabId != -1)
                    {
                        this.Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.PortalSettings.LoginTabId), true);
                    }
                    else
                    {
                        this.Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("Login"), true);
                    }
                }
            }
            if (!blnLayoutMode)
            {
                this.CollapseUnusedPanes();
            }
            if (this.Request.QueryString["error"] != null)
            {
                AddPageMessage(this, this.CRITICAL_ERROR, this.Server.HtmlEncode(this.Request.QueryString["error"]), ModuleMessage.ModuleMessageType.RedError);
            }
        }

        public void RegisterModuleActionEvent(int ModuleID, ActionEventHandler e)
        {
            this.ActionEventListeners.Add(new ModuleActionEventListener(ModuleID, e));
        }

        public ArrayList ActionEventListeners
        {
            get
            {
                if (this._actionEventListeners == null)
                {
                    this._actionEventListeners = new ArrayList();
                }
                return this._actionEventListeners;
            }
            set
            {
                this._actionEventListeners = value;
            }
        }

        public string SkinPath
        {
            get
            {
                return (this.TemplateSourceDirectory + "/");
            }
        }
    }
}

